﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 角色实体类。
 * 
 * 最后修改：2011-10-30
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 角色实体类。
    /// </summary>
    public class Role
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Role"/>。
        /// </summary>
        public Role()
        {
            this.RoleID = -1;
            this.SettingsID = SiteSettings.Current.SettingsID;
        }
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.Role"/>。
        /// </summary>
        /// <param name="reader"><see cref="T:System.Data.IDataReader"/>实例对象。</param>
        public Role(IDataReader reader)
        {
            this.SettingsID = reader.GetInt32("SettingsID");
            this.RoleID = reader.GetInt32("RoleID");
            this.RoleName = reader.GetString("RoleName");
            this.Description = reader.GetString("Description");
            this.IsHidden = reader.GetBoolean("IsHidden");
            this.LogoUrl = reader.GetString("LogoUrl");
            this.DisplayColor = reader.GetString("DisplayColor");
            this.SortOrder = reader.GetInt32("SortOrder");
            this.RoleLevel = reader.TGet<RoleLevel>("RoleLevel");
        }
        /// <summary>
        /// 获取或设置网站ID。
        /// </summary>
        public int SettingsID { get; set; }
        /// <summary>
        /// 获取或设置角色ID。
        /// </summary>
        public int RoleID { get; set; }
        /// <summary>
        /// 获取或设置角色名称。
        /// </summary>
        public string RoleName { get; set; }
        /// <summary>
        /// 获取或设置描述。
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 获取或设置是否隐藏。
        /// </summary>
        public bool IsHidden { get; set; }
        /// <summary>
        /// 获取或设置图片地址。
        /// </summary>
        public string LogoUrl { get; set; }
        /// <summary>
        /// 获取或设置显示颜色。
        /// </summary>
        public string DisplayColor { get; set; }
        /// <summary>
        /// 获取或设置角色排序。
        /// </summary>
        public int SortOrder { get; set; }
        /// <summary>
        /// 获取或设置角色等级。
        /// </summary>
        public RoleLevel RoleLevel { get; set; }
        /// <summary>
        /// 对比角色大小。
        /// </summary>
        /// <param name="role1">角色1。</param>
        /// <param name="role2">角色2。</param>
        /// <returns>对比角色大小，SortOrder越大越小。</returns>
        public static bool operator >(Role role1, Role role2)
        {
            return role1.RoleLevel > role2.RoleLevel || (role1.SortOrder > role2.SortOrder && role1.RoleLevel == role2.RoleLevel);
        }
        /// <summary>
        /// 对比角色大小。
        /// </summary>
        /// <param name="role1">角色1。</param>
        /// <param name="role2">角色2。</param>
        /// <returns>对比角色大小，SortOrder越大越小。</returns>
        public static bool operator <(Role role1, Role role2)
        {
            return role1.RoleLevel < role2.RoleLevel || (role1.SortOrder < role2.SortOrder && role1.RoleLevel == role2.RoleLevel);
        }
        /// <summary>
        /// 对比角色大小。
        /// </summary>
        /// <param name="role1">角色1。</param>
        /// <param name="role2">角色2。</param>
        /// <returns>对比角色大小，SortOrder越大越小。</returns>
        public static bool operator >=(Role role1, Role role2)
        {
            return role1.RoleLevel >= role2.RoleLevel || (role1.SortOrder >= role2.SortOrder && role1.RoleLevel == role2.RoleLevel);
        }
        /// <summary>
        /// 对比角色大小。
        /// </summary>
        /// <param name="role1">角色1。</param>
        /// <param name="role2">角色2。</param>
        /// <returns>对比角色大小，SortOrder越大越小。</returns>
        public static bool operator <=(Role role1, Role role2)
        {
            return role1.RoleLevel <= role2.RoleLevel || (role1.SortOrder <= role2.SortOrder && role1.RoleLevel == role2.RoleLevel);
        }

        internal static Role Administrator = new Role { RoleName = "Administrator", DisplayColor = "#ff0000", RoleLevel = RoleLevel.Administrator, SortOrder = 0, Description = "Website Administrator" };

        internal static Role Register = new Role { RoleName = "Register", DisplayColor = "#00ff00", RoleLevel = RoleLevel.Default, SortOrder = 0, Description = "For registers" };
    }
}
